package com.cn.wanxi.service;

import com.cn.wanxi.IO.CompanyModelIO;
import com.cn.wanxi.model.CompanyModel;
import com.cn.wanxi.util.IOReadWrite;
import com.cn.wanxi.util.Path;

import java.io.*;
import java.util.ArrayList;
import java.util.List;

/**
 * 服务逻辑层
 */
public class CompanyService {
    /**
     * 1.将所有数据存入记事本
     * 2.将记事本的数据读取出来
     * 3.封装到model里面
     * 每一个方法只做意见事情
     *
     * @return
     */
    public CompanyModel getCompanyModel() {
        /**
         * 1.首页需要查询当前文件是否存在，如果存在，则在当前文件夹里面创建文件，
         * 2.如果不存在，则创建文件夹，然后在创建文件
         */
        writeNote(Path.COMPANY_NAME.getPath());
        String result = new IOReadWrite().readNote(Path.COMPANY_NAME.getPath());
        return setData(result);
    }

    /**
     * @param result
     * @return
     */
    private CompanyModel setData(String result) {
        System.out.println(result);
        String[] results = result.split(Path.SPLIT.getPath());
        CompanyModel companyModel = new CompanyModel();
//        String[] keys={"logo","phone","ad"};
//        for (int i=0;i<results.length;i++){
//            companyModel.setField(keys[i],results[i]);
//        }
        companyModel.setLogo(results[0]);
        companyModel.setPhone(results[1]);
        companyModel.setAd(results[2]);
        companyModel.setNet(results[3]);
        companyModel.setEmail(results[4]);
        companyModel.setLinePhone(results[5]);
        companyModel.setAddress(results[6]);
        companyModel.setVersion(results[7]);
        companyModel.setIntroduce(results[8]);
        return companyModel;
    }

    /**
     * 写入记事本的方法
     * 1.每一个写入记事本的方法里面都有创建文件和文件夹。
     * 2.需要传入的文件名称fileName，需要写入的具体数据list
     * 3.这个方法的目的就是封装具体数据list
     *
     * @param fileName
     */
    private void writeNote(String fileName) {
        CompanyModelIO companyModelIO = new CompanyModelIO();
        List<String> list = new ArrayList<>();
        list.add(companyModelIO.getLogo());
        list.add(companyModelIO.getPhone());
        list.add(companyModelIO.getAd());
        list.add(companyModelIO.getNet());
        list.add(companyModelIO.getEmail());
        list.add(companyModelIO.getLinePhone());
        list.add(companyModelIO.getAddress());
        list.add(companyModelIO.getVersion());
        list.add(companyModelIO.getIntroduce());
        IOReadWrite.writeNote(fileName, list);
    }

}
